package com.sale.salesystem.controller;

import com.sale.salesystem.dto.DashboardQueryDTO;
import com.sale.salesystem.service.DashboardService;
import com.sale.salesystem.vo.DashboardVO;
import com.sale.salesystem.vo.ProductAnalysisVO;
import com.sale.salesystem.vo.ResultVO;
import com.sale.salesystem.vo.CustomerAnalysisVO;
import com.sale.salesystem.vo.ShopAnalysisVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 仪表板数据分析接口
 */
@RestController
@RequestMapping("/api/dashboard")
@RequiredArgsConstructor
@CrossOrigin
public class DashboardController {

    private final DashboardService dashboardService;

    /**
     * 获取分析数据
     * 
     * @param queryDTO 查询条件
     * @return 分析结果
     * 
     *         示例请求：
     *         POST /api/dashboard/analysis
     *         {
     *         "startTime": "2023-01-01",
     *         "endTime": "2023-12-31",
     *         "category": "T恤",
     *         "city": "上海",
     *         "shopId": 335,
     *         "gender": "男",
     *         "ageRange": "20-24"
     *         }
     * 
     *         示例响应：
     *         {
     *         "code": 200,
     *         "message": "success",
     *         "data": {
     *         "totalSales": 100000,
     *         "totalProfit": 30000,
     *         "orderCount": 1000,
     *         "salesVolume": 2000,
     *         "customerCount": 500,
     *         "salesTrend": [...],
     *         "profitTrend": [...],
     *         "orderTrend": [...],
     *         "volumeTrend": [...],
     *         "customerTrend": [...]
     *         }
     *         }
     */
    @PostMapping("/analysis")
    public ResultVO<DashboardVO> getAnalysisData(@RequestBody DashboardQueryDTO queryDTO) {
        return ResultVO.success(dashboardService.getAnalysisData(queryDTO));
    }

    /**
     * 获取所有产品类别
     * 
     * @return 产品类别列表
     * 
     *         示例请求：
     *         GET /api/dashboard/categories
     * 
     *         示例响应：
     *         {
     *         "code": 200,
     *         "message": "success",
     *         "data": ["T恤", "短裤", "牛仔裤", "裙子", "毛衣"]
     *         }
     */
    @GetMapping("/categories")
    public ResultVO<List<String>> getAllCategories() {
        return ResultVO.success(dashboardService.getAllCategories());
    }

    /**
     * 获取所有城市
     * 
     * @return 城市列表
     * 
     *         示例请求：
     *         GET /api/dashboard/cities
     * 
     *         示例响应：
     *         {
     *         "code": 200,
     *         "message": "success",
     *         "data": ["上海", "深圳", "成都", "西安", "重庆"]
     *         }
     */
    @GetMapping("/cities")
    public ResultVO<List<String>> getAllCities() {
        return ResultVO.success(dashboardService.getAllCities());
    }

    /**
     * 获取所有商店编号
     * 
     * @return 商店编号列表
     * 
     *         示例请求：
     *         GET /api/dashboard/shops
     * 
     *         示例响应：
     *         {
     *         "code": 200,
     *         "message": "success",
     *         "data": [335, 360, 430, 442, 496]
     *         }
     */
    @GetMapping("/shops")
    public ResultVO<List<Long>> getAllShopCodes() {
        return ResultVO.success(dashboardService.getAllShopCodes());
    }

    /**
     * 获取所有性别选项
     * 
     * @return 性别列表
     * 
     *         示例请求：
     *         GET /api/dashboard/genders
     * 
     *         示例响应：
     *         {
     *         "code": 200,
     *         "message": "success",
     *         "data": ["男", "女"]
     *         }
     */
    @GetMapping("/genders")
    public ResultVO<List<String>> getAllGenders() {
        return ResultVO.success(dashboardService.getAllGenders());
    }

    /**
     * 获取所有年龄范围
     * 
     * @return 年龄范围列表
     * 
     *         示例请求：
     *         GET /api/dashboard/age-ranges
     * 
     *         示例响应：
     *         {
     *         "code": 200,
     *         "message": "success",
     *         "data": ["<20", "20-24", "25-29", "30-34", "35-39"]
     *         }
     */
    @GetMapping("/age-ranges")
    public ResultVO<List<String>> getAllAgeRanges() {
        return ResultVO.success(dashboardService.getAllAgeRanges());
    }

    /**
     * 获取核心分析数据
     * 
     * @param queryDTO 查询条件
     * @return 分析结果
     */
    @PostMapping("/core")
    public ResultVO<DashboardVO> getCoreAnalysis(@RequestBody DashboardQueryDTO queryDTO) {
        DashboardVO dashboardVO = new DashboardVO();
        
        // 获取KPI数据
        DashboardVO.KpiDataVO kpiData = dashboardService.getKpiData(queryDTO);
        dashboardVO.setKpiData(kpiData);
        
        // 获取图表数据
        DashboardVO.ChartDataVO chartData = dashboardService.getChartData(queryDTO);
        dashboardVO.setChartData(chartData);
        
        return ResultVO.success(dashboardVO);
    }

    /**
     * 获取产品分析数据
     * 
     * @param queryDTO 查询条件
     * @return 产品分析数据
     */
    @PostMapping("/product")
    public ResultVO<ProductAnalysisVO> getProductAnalysis(@RequestBody DashboardQueryDTO queryDTO) {
        return ResultVO.success(dashboardService.getProductAnalysis(queryDTO));
    }

    /**
     * 获取客户分析数据
     * 
     * @param queryDTO 查询条件
     * @return 客户分析数据
     */
    @PostMapping("/customer")
    public ResultVO<CustomerAnalysisVO> getCustomerAnalysis(@RequestBody DashboardQueryDTO queryDTO) {
        return ResultVO.success(dashboardService.getCustomerAnalysis(queryDTO));
    }

    /**
     * 获取商店分析数据
     * 
     * @param queryDTO 查询条件
     * @return 商店分析数据
     */
    @PostMapping("/shop")
    public ResultVO<ShopAnalysisVO> getShopAnalysis(@RequestBody DashboardQueryDTO queryDTO) {
        return ResultVO.success(dashboardService.getShopAnalysis(queryDTO));
    }

    /**
     * 获取收入分析数据
     *
     * @param queryDTO 查询参数
     * @return 收入分析数据
     */
    @PostMapping("/income")
    public ResultVO<DashboardVO> getIncomeAnalysis(@RequestBody DashboardQueryDTO queryDTO) {
        return ResultVO.success(dashboardService.getIncomeAnalysis(queryDTO));
    }

    /**
     * 获取图表数据
     * 
     * @param dataSource 数据源
     * @param chartType 图表类型
     * @param filters 过滤条件
     * @return 图表数据
     */
    @GetMapping("/chart-data")
    public ResultVO<Map<String, Object>> getChartData(
            @RequestParam String dataSource,
            @RequestParam String chartType,
            @RequestParam(required = false) Map<String, Object> filters) {
        
        return ResultVO.success(dashboardService.getChartData(dataSource, chartType, filters));
    }
}